package com.wedimob.content;

/**
 * content type to retreive object extension.
 * 
 * @author melouadi
 * 
 */
public enum ContentType {
	TEXT, HTML, JSON, XML, PICTURE, SOUND, VIDEO, PDF, FLASH, BINARY;

	public static ContentType fromExtension(String extension) {
		if ("text".equals(extension) || "txt".equals(extension))
			return TEXT;

		if ("htm".equals(extension) || "html".equals(extension))
			return HTML;

		if ("json".equals(extension))
			return JSON;

		if ("xml".equals(extension))
			return XML;

		if ("jpg".equals(extension) || "jpeg".equals(extension)
				|| "png".equals(extension) || "gif".equals(extension)
				|| "tiff".equals(extension) || "bmp".equals(extension))
			return PICTURE;

		if ("mp3".equals(extension) || "rm".equals(extension)
				|| "wav".equals(extension) || "wma".equals(extension))
			return SOUND;

		if ("mp4".equals(extension) || "wmv".equals(extension) || "3gpp".equals(extension))
			return VIDEO;

		if ("pdf".equals(extension))
			return TEXT;

		if ("flv".equals(extension) || "webm".equals(extension))
			return FLASH;

		return BINARY;
	}
	
	public static String extractExtension(final String contentType) {
		int idxBegin = contentType.indexOf("/");
		if (idxBegin < 0) {
			idxBegin = 0;
		} else {
			idxBegin += 1;
		}

		int idxEnd = contentType.indexOf(";", idxBegin);
		if (idxEnd < 0) {
			idxEnd = contentType.length();
		}

		return contentType.substring(idxBegin, idxEnd);
	}

}
